package com.wangxi.boot9reg.controller;

import com.wangxi.boot9reg.entity.Product;
import com.wangxi.boot9reg.entity.User;
import com.wangxi.boot9reg.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        User u = mapper.selectByName(user.getUsername());
        if(u==null){
            mapper.insert(user);
            return 1;
        }
        return 2;
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u = mapper.selectByName(user.getUsername());
        if(u!=null){
            if(u.getPassword().equals(user.getPassword())){
                return 1;
            }
            return 3;
        }
        return 2;
    }

    @RequestMapping("/update")
    public void insertPro(@RequestBody Product product){
        mapper.insertPro(product);
    }

    @RequestMapping("/select")
    public List<Product> select(){
        return mapper.select();
    }

    @RequestMapping("/delete")
    public void delete(int id){
        mapper.delete(id);
    }

    @RequestMapping("/selectById")
    public Product selectById(int id){
        return mapper.selectPro(id);
    }

    @RequestMapping("/updatePro")
    public void updatePro(@RequestBody Product product){
        mapper.updatePro(product);
    }
}
